<?php
/**
 * 
 * @author Kartik
 *
 */
class Buyback_Resource_Offer_Amazon implements SeekableIterator, Countable, ArrayAccess
{
	const NOT_AVAILABLE = 1;
	
	const OTHER = "undefined error";
	
	protected $apiKey = "0E8S7VSHS30YDDCG2JR2";
	protected $secretKey = "ziK6hJ+PfcAL1zaMgatJF7hq9KDChnuJ5zF/SXEl";
	protected $countryCode = "US";
	
	protected $_AmazonConn;

	/**
	 * @var Buyback_Resources_Offer_Item
	 */
	protected $_items = array();
		
	/**
	 * @param string_array $isbnList
	 * @return Zend_Service_Amazon_Item/Zend_Service_Amazon_ResultSet
	 */
	public function getOffer($isbnList = array()){
		if(null == $isbnList)
		{
			return false;
		}
		
		$q = $this->getAmazonConnection();

		foreach($isbnList as $isbn){

			$item = $q->itemLookup(
				$isbn,
				array(
	    				"SearchIndex" 	=> "Books",
	    				"IdType"		=> "EAN",
	    			)
				);
				
			$count = 0;
			if($item instanceOf Zend_Service_Amazon_ResultSet){
				$item->rewind();
				while($item->valid() && $this->checkProductType($item->current()->ProductGroup)){
					$offerItem = new Buyback_Resource_Offer_Item(
												'A',
												$item->current(), 
												Doctrine_Model_PriceRulesAmazon::fetchOfferAMZ(
													array(
													'price' 	=>	$item->current()->Offers->LowestUsedPrice,
													'weight'	=>	$item->current()->Weight,
													'rank'		=>	$item->current()->SalesRank
												)),
												$item->current()->SalesRank
												);
					
					$this->_items[$offerItem->Isbn13] = $offerItem;
					$item->next();
					$count++;
				}
			}
			if($item instanceOf Zend_Service_Amazon_Item && $this->checkProductType($item->ProductGroup)){
				$offerItem = new Buyback_Resource_Offer_Item(
												'A',
												$item, 
												Doctrine_Model_PriceRulesAmazon::fetchOfferAMZ(
													array(
													'price' 	=>	$item->Offers->LowestUsedPrice,
													'weight'	=>	$item->Weight,
													'rank'		=>	$item->SalesRank
												)),
												$item->SalesRank 
												);
					
					$this->_items[$offerItem->Isbn13] = $offerItem;
			}
		}
		return $this->_items;
		
	}
	
	public function checkProductType($productType){
		switch ($productType){
			case "Book":
				return true;
				break;
			default:
				return false;
		}
	}
	
	public function setAmazonConnection(Zend_Service_Amazon $conn){
		$this->_AmazonConn = $conn;
	}
	
	public function getAmazonConnection(){
		if(null === $this->_AmazonConn){
			$this->setAmazonConnection(new Zend_Service_Amazon($this->apiKey, $this->countryCode, $this->secretKey));
		}
		return($this->_AmazonConn);
	}
	
/**
     * Does the given offset exist?
     *
     * @param string|int $key key
     * @return boolean offset exists?
     */
    public function offsetExists($key)
    {
        return isset($this->_items[$key]);
    }

    /**
     * Returns the given offset.
     *
     * @param string|int $key key
     * @return mixed
     */
    public function offsetGet($key)
    {
        return $this->_items[$key];
    }

    /**
     * Sets the value for the given offset.
     *
     * @param string|int $key
     * @param mixed $value
     */
    public function offsetSet($key, $value)
    {
        return $this->_items[$key] = $value;
    }

    /**
     * Unset the given element.
     *
     * @param string|int $key
     */
    public function offsetUnset($key)
    {
        unset($this->_items[$key]);
    }

    /**
     * Returns the current row.
     *
     * @return array|boolean current row 
     */
    public function current()
    {
        return current($this->_items);
    }

    /**
     * Returns the current key.
     *
     * @return array|boolean current key
     */
    public function key()
    {
        return key($this->_items);
    }

    /**
     * Moves the internal pointer to the next item and
     * returns the new current item or false.
     *
     * @return array|boolean next item
     */
    public function next()
    {
        return next($this->_items);
    }

    /**
     * Reset to the first item and return.
     *
     * @return array|boolean first item or false
     */
    public function rewind()
    {
        return reset($this->_items);
    }

    /**
     * Is the pointer set to a valid item?
     *
     * @return boolean valid item?
     */
    public function valid()
    {
        return current($this->_items) !== false;
    }

    /**
     * Seek to the given index.
     *
     * @param int $index seek index
     */
    public function seek($index)
    {
        $this->rewind();
        $position = 0;

        while ($position < $index && $this->valid()) {
            $this->next();
            $position++;
        }

        if (!$this->valid()) {
            throw new Exception('Invalid seek position');
        }
    }

    /**
     * Count the cart items
     *
     * @return int row count
     */
    public function count()
    {
        return count($this->_items);
    }
}